package com.jeesite.modules.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class GeneratePoster {
    private BufferedImage image;

    //生成图片文件
    @SuppressWarnings("restriction")
    public void createImage(String fileLocation) {
        BufferedOutputStream bos = null;
        if(image != null){
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
               JPEGEncodeParam jpegEncodeParam = encoder.getDefaultJPEGEncodeParam(image);
                jpegEncodeParam.setQuality(1f,false);
                encoder.encode(image,jpegEncodeParam);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(bos!=null){//关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void graphicsGeneration(String imgurl,String code,String exclusivePosterPath) {
        int textHeight = 60; //编码文字所在区域的高度
        int textMarginLeft = 5; //编码文字左缩进
        int textMarginTop = 40; //编码文字上缩进
        int fontSize = 45; //编码文字大小
        BufferedImage bimg = null;
        try {
            bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
            int imageWidth = bimg.getWidth();
            int imageHeight = bimg.getHeight();
            image = new BufferedImage(imageWidth, imageHeight+textHeight, BufferedImage.TYPE_INT_RGB);
            Graphics mainPic = image.getGraphics();

            if(bimg!=null){
                mainPic.drawImage(bimg, 0, 0, imageWidth, imageHeight, null);
                mainPic.dispose();
            }

            Graphics2D tip = image.createGraphics();
            //设置区域颜色
            tip.setColor(new Color(0, 0, 0));
            //填充区域并确定区域大小位置
            tip.fillRect(0, imageHeight, imageWidth, textHeight);
            //设置字体颜色，先设置颜色，再填充内容
            tip.setColor(Color.white);
            //设置字体
            Font tipFont = new Font("宋体", Font.BOLD, fontSize);
            tip.setFont(tipFont);
            tip.drawString("邀请码："+code, textMarginLeft, imageHeight+textMarginTop);



        } catch (Exception e) {}


        //***********************设置下面的提示框






      /*  //***********************设置下面的按钮块
        //设置字体颜色，先设置颜色，再填充内容
        tip.setColor(Color.black);
        tip.drawString("您可以选择的操作：", 20, btns_2_top);
        tip.drawString("下面的小图标：", 20, shops_2_top);
        //***********************按钮
        Font btnFont = new Font("宋体", Font.BOLD, 14);
        Graphics2D btn1 = image.createGraphics();
        btn1.setColor(new Color(41,192 , 50));//#29C65A
        btn1.fillRect(10, btn1_2_top, W_btn, H_btn);
        btn1.setColor(Color.BLACK);
        btn1.drawRect(10, btn1_2_top, W_btn, H_btn);
        //btn1 文本
        btn1.setColor(Color.white);
        btn1.setFont(btnFont);
        btn1.drawString("单击我啊", 120, btn1_2_top+(H_btn/2)+5);

        Graphics2D btn2 = image.createGraphics();
        btn2.setColor(new Color(141,120 , 22));//#29C65A
        btn2.fillRect(10, btn2_2_top, W_btn, H_btn);
        btn2.setColor(Color.BLACK);
        btn2.drawRect(10, btn2_2_top, W_btn, H_btn);
        //btn2文本
        btn2.setColor(Color.white);
        btn2.setFont(btnFont);
        btn2.drawString("单击我啊", 120, btn2_2_top+(H_btn/2)+5);*/

       // createImage("d:\\hehe.jpg");
        createImage(exclusivePosterPath);

    }

    public static void main(String[] args) {
        GeneratePoster cg = new GeneratePoster();
        try {
            cg.graphicsGeneration( "D:\\1.png","123456","d:\\hehe.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}